package com.bgh.dynobot;

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class CreateVehicle extends Activity implements OnClickListener {
	
	private Button CreateButton = null;
	private Button CancelButton = null;
	private DynoDbAdapter DbHelper;
	
	private EditText et = null;
	private Spinner sp = null;
	private TextView tv = null;
	
	private ViewFlipper viewFlipper;
	private Animation slideLeftIn;
	private Animation slideLeftOut;
	private Animation slideRightIn;
    private Animation slideRightOut;
	
	public String vehicleName, vehicleDesc, vehicleWeight, weightUnit, vehiclePicURI = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.create_vehicle);
        
        CreateButton = (Button) findViewById(R.id.createVehicle);
        CreateButton.setOnClickListener(this);
        
        CancelButton = (Button) findViewById(R.id.createVehicle);
        CancelButton.setOnClickListener(this);
        
        Spinner spinner = (Spinner) this.findViewById(R.id.editWeightUnit);
        
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
                        this, android.R.layout.simple_spinner_item);
 
        adapter.add("Kilograms");
        adapter.add("Pounds");
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        
        ImageView image =  (ImageView)this.findViewById(R.id.vehiclePic);
        Uri imageUri = Uri.parse("/sdcard/media/pics/Ute/ute2.JPG");          
        image.setImageURI(imageUri);     
        
      
        TextView tv =  (TextView)this.findViewById(R.id.vehiclePicURI);
        tv.setText("/sdcard/media/pics/Ute/ute2.JPG");          
        tv.setVisibility(0);
        tv.setHeight(0);
        
        viewFlipper = (ViewFlipper)findViewById(R.id.vehicle_flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);   
    }
	
	public void onClick(View v) {
		if (v == CreateButton) {
			
			et = (EditText) this.findViewById(R.id.editName);
			vehicleName = et.getText().toString();
			
			et = (EditText) this.findViewById(R.id.editDesc);
			vehicleDesc = et.getText().toString();
			
			et = (EditText) this.findViewById(R.id.editWeight);
			vehicleWeight = et.getText().toString();
			
			sp = (Spinner) this.findViewById(R.id.editWeightUnit);
			weightUnit = (String)sp.getSelectedItem();
			
			TextView tv =  (TextView)this.findViewById(R.id.vehiclePicURI);
	        vehiclePicURI = (String) tv.getText();         
			
			DbHelper = new DynoDbAdapter(this);
	        DbHelper.open();
	        DbHelper.insertVehicle(vehicleName, vehicleDesc, Integer.parseInt(vehicleWeight), weightUnit, vehiclePicURI);
	        Toast.makeText(this, "Vehicle created", Toast.LENGTH_LONG).show();
	        DbHelper.close();
		}
		
		if(v == CancelButton){
			viewFlipper.setInAnimation(slideRightIn);
            viewFlipper.setOutAnimation(slideRightOut);
    		viewFlipper.showNext();	
		}
	}
	
	public void createVehicle(){
		
	}

}
